package top.mcwebsite.playandroid.lib.markdown.render

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckBox
import androidx.compose.material.icons.filled.CheckBoxOutlineBlank
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
import org.intellij.markdown.ast.getTextInNode
import org.intellij.markdown.flavours.gfm.GFMTokenTypes
import top.mcwebsite.playandroid.lib.markdown.BlockVerticalPadding

@Composable
fun UnOrderedList(
    astNode: ASTNode,
    markdownState: MarkdownState,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier.padding(vertical = BlockVerticalPadding)
    ) {
        astNode.children.forEach {
            when (it.type) {
                MarkdownElementTypes.LIST_ITEM -> UnOrderedListItems(astNode = it, markdownState = markdownState)
            }
        }
    }
}

@Composable
fun UnOrderedListItems(astNode: ASTNode, markdownState: MarkdownState) {
    Column {
        if (astNode.findChildOfType(GFMTokenTypes.CHECK_BOX) != null) {
            CheckBox(astNode = astNode, markdownState = markdownState)
        } else {
            UnOrderedListItem(astNode = astNode, markdownState = markdownState)
        }
        astNode.findChildOfType(MarkdownElementTypes.UNORDERED_LIST)?.let {
            UnOrderedList(astNode = it, markdownState = markdownState, modifier = Modifier.padding(start = 30.dp))
        }
        astNode.findChildOfType(MarkdownElementTypes.ORDERED_LIST)?.let {
            OrderedList(astNode = it, markdownState = markdownState, modifier = Modifier.padding(start = 30.dp))
        }
    }
}


@Composable
fun UnOrderedListItem(astNode: ASTNode, markdownState: MarkdownState) {
    Row {
        Box(
            modifier = Modifier.size(20.dp, 20.dp),
            contentAlignment = Alignment.Center
        ) {
            Box(
                modifier = Modifier
                    .size(4.dp)
                    .background(MaterialTheme.colorScheme.onSurface, shape = CircleShape)
            )
        }
        astNode.findChildOfType(MarkdownElementTypes.PARAGRAPH)?.let {
            RichText(
                markdownState = markdownState,
                astNode = it,
                style = TextStyle(),
                modifier = Modifier.padding(start = 5.dp)
            )
        }
    }
}

@Composable
fun CheckBox(astNode: ASTNode, markdownState: MarkdownState) {
    val isCheckedContent =
        astNode.findChildOfType(GFMTokenTypes.CHECK_BOX)?.getTextInNode(markdownState.text) ?: return
    val isChecked = isCheckedContent.trim() != "[ ]"
    Row {
        Icon(
            if (isChecked) {
                Icons.Default.CheckBox
            } else {
                Icons.Default.CheckBoxOutlineBlank
            },
            contentDescription = "checkbox", tint = Color.Gray
        )
        astNode.findChildOfType(MarkdownElementTypes.PARAGRAPH)?.let {
            RichText(markdownState = markdownState, astNode = it, style = TextStyle(), modifier = Modifier)
        }
    }
}

